//
//  ViewController.m
//  饼状图
//
//  Created by tanqilong on 16/8/23.
//  Copyright © 2016年 tanqilong. All rights reserved.
//

#import "ViewController.h"
#import "CMPieChartMap.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(150, 100, 50, 50)];
//    view.backgroundColor = [UIColor blackColor];
//    [self.view addSubview:view];
//    //测试画一个饼状图
//    UIBezierPath * path = [[UIBezierPath alloc] init];
//    [path moveToPoint:CGPointMake(185, 100)];
//    [path addArcWithCenter:CGPointMake(150, 100) radius:25 startAngle:0 endAngle:2*M_PI clockwise:YES];
//    CAShapeLayer * shapLayer = [CAShapeLayer layer];
//    shapLayer.strokeColor = [[UIColor redColor] CGColor];
//    shapLayer.fillColor = [[UIColor clearColor] CGColor];
//    shapLayer.lineWidth = 25;
////    shapLayer.strokeStart = 0;
////    shapLayer.strokeEnd = 0.9;
//    shapLayer.path = path.CGPath;
//    [self.view.layer addSublayer:shapLayer];
    
    //测试画多个饼状图
//    CAShapeLayer * containerLayer = [CAShapeLayer new];
//    containerLayer.frame = CGRectMake(0, 0,self.view.bounds.size.width,self.view.bounds.size.width);
//    float startPercent = 0;
//    float endPercent = 0;
//    
//    for (int i =0; i < 3; i++) {
//        startPercent = 1.0 / 3.0 * i;
//        endPercent += 1.0 / 3.0;
//        UIColor * color;
//        if (i == 0) {
//            color = [UIColor yellowColor];
//        } else if (i == 1) {
//            color = [UIColor redColor];
//        } else {
//            color = [UIColor blueColor];
//        }
//        CAShapeLayer * arcLayer = [self generateLayerWidth:self.view.bounds.size.width / 3.0 StartPercent:startPercent EndPercent:endPercent Color:color];
//        [containerLayer addSublayer:arcLayer];
//        
//        [arcLayer drawInContext:UIGraphicsGetCurrentContext()];
//    }
//    
//
//    
////    CAShapeLayer * arcLayer = [self generateLayerWidth:100 StartPercent:0.5 EndPercent:0.9 Color:[UIColor yellowColor]];
////    [containerLayer addSublayer:arcLayer];
//    [self.view.layer addSublayer:containerLayer];
//    
//    UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.width), YES, self.view.layer.contentsScale);
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    [self.view.layer renderInContext:context];
//    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
//    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
////    imageView.layer.shouldRasterize = YES;
//    imageView.image = image;
//
//    [self.view addSubview:imageView];
    
//    self.view.backgroundColor = [UIColor grayColor];
    
    CMPieChartMode * mode = [CMPieChartMode new];
    mode.color = [UIColor redColor];
    mode.percent = 0.3;
    CMPieChartMode * mode1 = [CMPieChartMode new];
    mode1.color = [UIColor yellowColor];
    mode1.percent = 0.5;
    CMPieChartMode * mode2 = [CMPieChartMode new];
    mode2.color = [UIColor blueColor];
    mode2.percent = 0.2;
    
    CMPieChartMap * map = [[CMPieChartMap alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.width)];
    map.backgroundColor = [UIColor grayColor];
    map.modeArray = @[mode,mode1,mode2];
    [self.view addSubview:map];

}

-(CAShapeLayer *)generateLayerWidth:(CGFloat)witdh StartPercent:(CGFloat)startPercent EndPercent:(CGFloat)endPercent Color:(UIColor *)color {
    UIBezierPath * path = [UIBezierPath new];
//    [path moveToPoint:CGPointMake(self.view.bounds.size.width, self.view.bounds.size.width / 2.0)];
    [path addArcWithCenter:CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.width/2.0) radius:self.view.bounds.size.width/2.0 - witdh/2.0 startAngle:-M_PI_2 endAngle:3 * M_PI_2 clockwise:YES];
    CAShapeLayer * shapLayer = [CAShapeLayer layer];
    shapLayer.strokeColor = [color CGColor];
//    shapLayer.shouldRasterize = YES;
    shapLayer.fillColor = nil;
    shapLayer.lineWidth = witdh;
    shapLayer.strokeStart = startPercent; //起始百分比
    shapLayer.strokeEnd = endPercent; //结束百分比
    shapLayer.path = path.CGPath;
    return shapLayer;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
